<?php
namespace App\Http\Controllers\Mp;
use Illuminate\Http\Request;
use Overtrue\LaravelWeChat\Facade;

/**
 * Class WxLoginController
 * @package App\Http\Controllers\Mp
 * @author zjw
 * 微信绑定
 */
class WxBindController extends BaseController{
    public function store(Request $request){
        $app = Facade::miniProgram();
        $session = $app->auth->session($request->code);
        if(!array_get($session,'errcode')){
            $user = \Auth::user();
            if ($user->mp_openid) {
                return $this->fail('该账号已绑定其他微信,无法使用');
            }
            $user->mp_openid = array_get($session,'openid');
            $user->unionid = array_get($session,'unionid');
            $user->session_key = array_get($session,'session_key');
            $user->save();
            return [];
        } else {
            return $this->fail('绑定失败');
        }

    }
}
